package com.example.calendertodo;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.SearchView;

import com.example.calendertodo.adapter.MyAdapter;
import com.example.calendertodo.beam.Todo;
import com.example.calendertodo.util.TodoDbOpenHelper;

import java.util.ArrayList;
import java.util.List;

public class TodoListActivity extends AppCompatActivity {
    private RecyclerView mRecyclerView;
    private List<Todo> mTodo;
    private MyAdapter mMyAdapter;
    private TodoDbOpenHelper mTodoDbOpenHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_todo_list);
        initView();
        initEvent();
    }

    protected void onResume() {
        //在进行对数据库的数据更改后（添加 删除 更改），应该在页面中刷新数据，否则需要重新加载页面才能显示更改的数据
        super.onResume();
        refreshDataFromDB(); //重新更新数据
    }

    private void initEvent() { //初始化操作
        mTodo= new ArrayList<>();
        mTodoDbOpenHelper = new TodoDbOpenHelper(this);
        mMyAdapter = new MyAdapter(this,mTodo); //调用构造器传入数据
        mMyAdapter.setHasStableIds(true);
//        mRecyclerView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));
        mRecyclerView.setAdapter(mMyAdapter);//为视图设置适配器
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);//线性布局
        mRecyclerView.setLayoutManager(layoutManager);

    }

    private void refreshDataFromDB() {
        mTodo = getDataFromDB(); //调用获取数据的方法来获取更新后的数据显示在主页面
        mMyAdapter.refreshData(mTodo);
    }
    private List<Todo> getDataFromDB() { //获取全部的数据，无限制条件
        return mTodoDbOpenHelper.queryAllFromDb();
    }

    private void initView() {
        mRecyclerView = findViewById(R.id.rv_list);
    }



    public void add(View view) {
        Intent intent = new Intent(this, AddActivity.class);
        startActivity(intent);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_list,menu); //绑定菜单

        //绑定菜单搜索框
        SearchView searchView =(SearchView) menu.findItem(R.id.menu_search_list).getActionView();
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String s) {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String s) { //根据文本框中的字符进行查找
                mTodo = mTodoDbOpenHelper.queryFromDbByTitle(s);
                mMyAdapter.refreshData(mTodo);
                return true;
            }
        });
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {

            case R.id.menu_switch_list:
                //转换视图
                Intent intent = new Intent(this, MainActivity.class);
                startActivity(intent);
                break;
            case R.id.menu_count_list:
                //切换到统计视图
                Intent intent1 = new Intent(this, CountActivity.class);
                startActivity(intent1);
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    public void refreshActivity(View view) {
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
    }
}